home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13330 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  69 lines

  1. Newsgroups: comp.unix.sco.programmer,comp.lang.c
  2. Path: quest1.questconsult.com!jrm
  3. From: jrm@quest1.questconsult.com (John Moyer)
  4. Subject: Re: Storing 20 character numbers
  5. Message-ID: <DpEMIA.GDE@quest1.questconsult.com>
  6. Date: Fri, 5 Apr 1996 19:37:22 GMT
  7. References: <DpB3sz.J9n@idm.com>
  8. Organization: Quest Consultants Inc.
  9.  
  10. In article <DpB3sz.J9n@idm.com> eck@idm.com (Eric Kurbat) writes:
  11. >Hello folks!
  12. >    I've got a question that has probably been asked before, but I need
  13. >to ask it again... I apologize in advance for any redundancy.
  14. >
  15. >    Anyway, the question is, how do you store a 20 character integer
  16. >number in a non-character variable in C without losing any of the digits?
  17. >I did a little bit of playing around and a long double loses precision after
  18. >about 16 digits. Does anyone have any ideas?
  19. >
  20. >Regards,
  21. >Eric
  22.  
  23. Are you saying that you need to do arithmetic with integers greater than
  24. 64 bits? If so, you need a bignum arithmetic package. One was posted to
  25. one of the source code groups a few years ago. If you can not find it
  26. elsewhere, I could e-mail it to you.
  27.  
  28. If you wish to do some assembly programming, Binary Coded Decimal is a
  29. good representation for this sort of thing with lots of special opcodes
  30. to support the arithmetic in Intel CPUs. Packed Binary Coded Decimal
  31. can store 2 decimal digits per byte.
  32.  
  33. A single unpacked BCD digit may be added this way in C if a and b contain
  34. the digits to be added with the result in c and carry.
  35.  
  36.  
  37. unsigned char a, b, c;
  38. int carry;
  39.  
  40. c = a+b;
  41. if ( c >= 10 )
  42.   {
  43.   c -= 10;
  44.   carry = 1; 
  45.   }
  46.   
  47. Then the carry needs to be added to the next most significant digit.
  48. There exists an opcode to do an integer add with carry that would
  49. avoid the if().
  50.  
  51. I hope this helps.
  52.  
  53. John
  54.  
  55.  
  56.  
  57. >--
  58. >----------------------------------------------------------------------------
  59. > "A cow can't whinny and horse has no udder.   | Eric Kurbat  :  eck@idm.com
  60. >  Up is down and sideways is straight ahead!"  |                 eck@wwa.com 
  61. >----------------------------------------------------------------------------
  62.  
  63.  
  64. -- 
  65. John Moyer                            http://www.questconsult.com/~jrm/
  66. Quest Consultants Inc. (tm)           jrm@questconsult.com         KC5GSX
  67. P.O. Box 721387                       (405) 329-7475
  68. Norman, Oklahoma 73070-8069, USA      Fax: (405) 329-7734
  69.